﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LiveResults.Model;

namespace LiveResults
{
    public partial class FrmMonitor : Form
    {
        private FrmMonitor()
        {
            InitializeComponent();
        }

        protected EventSoftware m_Event;
        protected MainApp.logDelegate m_Logger;
        protected Event m_ModelEvent;
        LiveResults.OnlineLib.LiveServer m_Server;
        public FrmMonitor(EventSoftware soft, MainApp.logDelegate logger) : this()
        {
            m_Event = soft;
            m_Logger = logger;
            m_ModelEvent = new Event();
            m_ModelEvent.ClassChanged += new Event.ClassChangedHandler(m_ModelEvent_ClassChanged);
            m_ModelEvent.RunnerChanged += new Event.RunnerChangedHandler(m_ModelEvent_RunnerChanged);

            m_Server = new LiveResults.OnlineLib.LiveServer(MainApp.CurrentProject.EventID, MainApp.CurrentProject.Password);
            m_Server.LogMsg += new LiveResults.OnlineLib.LiveServer.LogMsgHandler(m_Server_LogMsg);
        }

        void m_Server_LogMsg(string msg)
        {
            m_Logger(msg);
        }

        void m_ModelEvent_RunnerChanged(Runner r)
        {
            m_Logger("Runner changed: " + r.FirstName + " " + r.LastName);
            m_Server.QueueUpload(r.OnlineResult);
        }

        void m_ModelEvent_ClassChanged(EventClass cl)
        {
            m_Logger("Class Changed: " + cl.ClassName);
            m_Server.QueueUpload(cl.OnlineClass);
        }

        private void FrmMonitor_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_Server.Dispose();
        }

    }
}
